package com.common.utils;


import org.springframework.util.FileCopyUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * Created by gxq on 2018/5/8.
 * 文件工具类
 */
public class FileUtils {



    /**
     * 文件后缀
     *
     * @param fileName
     * @return
     */
    public static String getFileSuffixWithoutPoint(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 2, fileName.length()).toLowerCase();
    }

    /**
     * 文件后缀
     *
     * @param fileName
     * @return
     */
    public static String getFileSuffixWithPoint(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
    }


    /**
     * 文件复制
     * @param in
     * @param filePath
     * @param fileName
     */
    public static void copy(InputStream in, String filePath, String fileName) {
        File folder;
        String dir;
        FileOutputStream out = null;
        try {
            folder = new File(filePath);
            if (!folder.exists()) folder.mkdirs();
            dir = String.format("%s%s", filePath, fileName);
            out = new FileOutputStream(new File(dir));
            FileCopyUtils.copy(in, out);
        } catch (Exception e) {

        } finally {
            try {
                if (in != null)
                    in.close();
                if (out != null){
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
    }

    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                System.out.println("删除单个文件" + fileName + "成功！");
                return true;
            } else {
                System.out.println("删除单个文件" + fileName + "失败！");
                return false;
            }
        } else {
            System.out.println("删除单个文件失败：" + fileName + "不存在！");
            return false;
        }
    }

    public static String RenameToUUID(String fileName) {
        return UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
    }


    public static int fileType(String fileName) {
        if (fileName == null) {
            fileName = "文件名为空！";
            return 500;

        } else {
            // 获取文件后缀名并转化为写，用于后续比较
            String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
            // 创建图片类型数组0
            String img[] = { "bmp", "jpg", "jpeg", "png", "tiff", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd",
                    "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "wmf" };
            for (int i = 0; i < img.length; i++) {
                if (img[i].equals(fileType)) {
                    return 0;
                }
            }

            // 创建文档类型数组1
            String document[] = { "txt", "doc", "docx", "xls", "htm", "html", "jsp", "rtf", "wpd", "pdf", "ppt" };
            for (int i = 0; i < document.length; i++) {
                if (document[i].equals(fileType)) {
                    return 1;
                }
            }
            // 创建视频类型数组2
            String video[] = { "mp4", "avi", "mov", "wmv", "asf", "navi", "3gp", "mkv", "f4v", "rmvb", "webm" };
            for (int i = 0; i < video.length; i++) {
                if (video[i].equals(fileType)) {
                    return 2;
                }
            }
            // 创建音乐类型数组3
            String music[] = { "mp3", "wma", "wav", "mod", "ra", "cd", "md", "asf", "aac", "vqf", "ape", "mid", "ogg",
                    "m4a", "vqf" };
            for (int i = 0; i < music.length; i++) {
                if (music[i].equals(fileType)) {
                    return 3;
                }
            }

        }
        //4
        return 99;
    }




}
